package com.wisewin.pose.util.token;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class TokenUtil {

    final static Logger log = LoggerFactory.getLogger(TokenUtil.class);

    //根据账号密码  IP  有效期 生成token
    public static String getToken(String account, String password) {
        String token = JWT.create().withAudience(account).sign(Algorithm.HMAC256(password));
        return token;
    }

    /**
     *@Description: token验证
     *@author: wsx
     *@date: 2021/8/17 11:55
     */
    public boolean jwtVerifier(String token){
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("xdzx2021")).build();
        try {
            jwtVerifier.verify(token);
            log.info("========token验证成功");
            return true;
        } catch (JWTVerificationException e) {
            log.info("========token验证失败");
            return false;
        }
    }

    public static void main(String []args){
        TokenUtil tokenUtil = new TokenUtil();
        String token = tokenUtil.getToken("xdzx","xdzx2021");
        log.info("=====token======="+token);
        tokenUtil.jwtVerifier(token);
        List<String> strList = JWT.decode(token).getAudience();
        for(String str : strList){
            log.info("=====Audience======="+str);
        }
    }
}
